<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="求购书籍" prop="buyName">
        <el-input
          v-model="queryParams.buyName"
          placeholder="请输入求购书籍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书本类别" prop="buyType">
        <el-cascader placeholder="请选择求购书籍的类别" 
          v-model="queryParams.buyType" 
          :options="categoryList" 
          :props="defaultBookTypeProps" 
          filterable clearable />
      </el-form-item>
      <el-form-item label="求购价格" prop="buyPrice">
        <el-input
          v-model="queryParams.buyPrice"
          placeholder="请输入求购此书籍出的价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:buyBook:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:buyBook:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:buyBook:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:buyBook:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buyBookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="求购学生" align="center" prop="uid" />
      <el-table-column label="求购书籍" align="center" prop="buyName" />
      <el-table-column label="求购价格" align="center" prop="buyPrice" />
      <el-table-column label="书籍类别" align="center" prop="buyTypeName" />
      <el-table-column label="求购数量" align="center" prop="buyCount" />
      <el-table-column label="是否展示" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="求购详细描述" align="center" prop="buyDesc" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:buyBook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:buyBook:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改求购信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="求购学生学号" prop="uid">
              <el-input v-model="form.uid" placeholder="请输入发表此求购信息的用户id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="求购书籍" prop="buyName">
              <el-input v-model="form.buyName" placeholder="请输入求购书籍名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="求购价格" prop="buyPrice">
              <el-input v-model="form.buyPrice" placeholder="请输入求购此书籍出的价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="求购数量" prop="buyCount">
              <el-input v-model="form.buyCount" placeholder="请输入求购数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="书本类别">
              <el-cascader placeholder="请选择求购书籍的类别" 
              v-model="form.buyType" 
              :options="categoryList" 
              :props="defaultBookTypeProps" 
              filterable clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="书籍图片">
              <ImageUpload v-model="form.imageUrl"></ImageUpload>
            </el-form-item> 
          </el-col>
          <el-col :span="24">
            <el-form-item label="求购详细描述" prop="buyDesc">
              <editor v-model="form.buyDesc" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuyBook, getBuyBook, delBuyBook, addBuyBook, updateBuyBook, changeBuyStatus } from "@/api/product/buyBook";
import { listCategoryWithTree } from "@/api/product/category";

export default {
  name: "BuyBook",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 求购信息表格数据
      buyBookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uid: null,
        buyName: null,
        buyPrice: null,
        buyType: null,
        buyCount: null,
        status: null,
        buyDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        uid: [
          { required: true, message: "求购学生的学号不能为空", trigger: "blur" }
        ],
        buyName: [
          { required: true, message: "求购书籍名称不能为空", trigger: "blur" }
        ],
        buyPrice: [
          { required: true, message: "求购价格不能为空", trigger: "blur" }
        ],
        buyCount: [
          { required: true, message: "求购数量不能为空", trigger: "blur" }
        ],
      },
      // 书籍分类表格数据
      categoryList: [],
      defaultBookTypeProps: {
        value: "id",
        label: "catName",
        children: "children",
        expandTrigger: "true",
        checkStrictly: "true"
      }
    };
  },
  created() {
    this.getList();
    this.loadingBookTypeTree();
  },
  methods: {
    /** 查询求购信息列表 */
    getList() {
      this.loading = true;
      listBuyBook(this.queryParams).then(response => {
        // console.log(response);
        this.buyBookList = response.data.list;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        buyName: null,
        buyPrice: null,
        buyType: null,
        buyCount: null,
        status: null,
        buyDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams);
      let buyType = this.queryParams.buyType;
      if(buyType != null && buyType instanceof Array){
        this.queryParams.buyType = buyType[buyType.length-1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加求购信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuyBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改求购信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // console.log(this.form);
      let buyType = this.form.buyType;
      if(buyType != null && buyType instanceof Array){
        this.form.buyType = buyType[buyType.length-1];
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuyBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuyBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除求购信息编号为"' + ids + '"的数据项？').then(function() {
        return delBuyBook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/buyBook/export', {
        ...this.queryParams
      }, `buyBook_${new Date().getTime()}.xlsx`)
    },
    // 书籍上架状态修改
    handleStatusChange(row) {
      let text = row.status === 0 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"书籍"' + row.buyName + '"的求购吗？').then(function() {
        return changeBuyStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },
    // 加载书籍类别的树形菜单
    loadingBookTypeTree(){
      listCategoryWithTree().then(response => {
        this.categoryList = response.data;
        this.categoryList.push({id:"0",catName:"暂无分类",catLevel:0,status:"0"})
        // console.log(response);
      });
    }
  }
};
</script>
